package com.feibi.study.UI

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.FragmentTransaction
import com.feibi.study.Basic.BasicActivity
import com.feibi.study.Basic.BasicFragment
import com.feibi.study.R
import com.feibi.study.UI.Fragments.HomeFragment
import kotlinx.android.synthetic.main.activity_frame.*
import java.util.ArrayList

class FrameActivity : BasicActivity() {
    private var fragmentBeanList: ArrayList<BasicFragment> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_frame)
        initFragment()
        tv_home.setOnClickListener { switchFragment(0) }
        tv_fri.setOnClickListener { switchFragment(1) }
        tv_art.setOnClickListener { switchFragment(2) }
        tv_me.setOnClickListener { switchFragment(3) }

    }

    fun initFragment() {
        fragmentBeanList.add(HomeFragment())
        fragmentBeanList.add(HomeFragment())
        fragmentBeanList.add(HomeFragment())
        fragmentBeanList.add(HomeFragment())
        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        ft.add(R.id.fl_layout, fragmentBeanList[0])
        fragmentBeanList[1].let { ft.add(R.id.fl_layout, it).hide(it) }
        fragmentBeanList[2].let { ft.add(R.id.fl_layout, it).hide(it) }
        fragmentBeanList[3].let { ft.add(R.id.fl_layout, it).hide(it) }
        ft.commit()
        switchFragment(0) //默認打開
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    fun switchFragment(index: Int) {
        tv_home.setTextColor(if(index==0) Color.BLUE else Color.BLACK)
        tv_fri.setTextColor(if(index==1) Color.BLUE else Color.BLACK)
        tv_art.setTextColor(if(index==2) Color.BLUE else Color.BLACK)
        tv_me.setTextColor(if(index==3) Color.BLUE else Color.BLACK)

        val targetFragment = fragmentBeanList[index]
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        for (item in fragmentBeanList) {
            if (item == targetFragment) transaction.show(item) else transaction.hide(item)
        }
        transaction.commit()
    }

}